<?php

class database extends xpfw {

    private $sql, $fields, $values, $connection;

    public function __construct($server = 'localhost', $username = '', $password = '', $newlink = '') {
        if (!$server || !$username || !$password || !$newlink) {
            switch (environment) {
                case 'development':
                    $username = 'root';
                    $password = 'senha';
                    $newlink = 'laiss';
                    break;
                case 'test':
                    $username = 'eteotcom';
                    $password = '$Professores2012';
                    $newlink = 'eteotcom_laiss';
                    break;
                case 'production':
                    $username = '';
                    $password = '';
                    $newlink = '';
                    break;
            }
        }
        $connection = mysql_connect($server, $username, $password);
        mysql_select_db($newlink, $connection);

        if (!$connection)
            $this->log('Connection [' . mysql_error() . ']', 500);
        else
            $this->setConnection($connection);
    }

    public function getInsertedId() {
        return mysql_insert_id($this->getConnection());
    }

    public function getConnection() {
        return $this->connection;
    }

    public function setConnection($connection) {
        mysql_set_charset('latin1');
        $this->connection = $connection;
    }

    public function getSql() {
        return $this->sql;
    }

    public function clearSql() {
        $this->sql = '';
    }

    public function setSql($sql) {
        $this->sql = $this->getSql() . $sql;
    }

    public function getFields() {
        return $this->fields;
    }

    public function setFields($fields) {
        $this->fields = $fields;
    }

    public function getValues() {
        return $this->values;
    }

    public function setValues($values) {
        $this->values = $values;
    }

    public function select($fields = '*') {
        $this->clearSql();
        $this->setSql('SELECT ' . $fields . ' ');

        return $this;
    }

    public function delete($table) {
        $this->clearSql();
        $this->setSql('DELETE FROM ' . $table . ' ');

        return $this;
    }

    public function insert($table, array $register) {
        $this->clearSql();
        $this->setSql('INSERT INTO ' . $table . ' (' . join(',', array_keys($register)) . ') ');
        $this->setSql('VALUES (\'' . join("','", $register) . '\') ');

        return $this;
    }

    public function update($table, array $register) {
        $this->clearSql();
        $this->setSql('UPDATE ' . $table . ' SET ');

        $sets = '';
        foreach ($register as $field => $value) {
            $sets .= "$field = '$value', ";
        }

        $this->setSql(substr($sets, 0, -2));

        return $this;
    }

    public function from($table) {
        $this->setSql('FROM ' . $table . ' ');
        return $this;
    }

    public function join($table, $condition = '') {
        $this->setJoinType('JOIN', $table, $condition);
        return $this;
    }

    protected function setJoinType($type, $table, $condition = '') {
        $this->setSql($type . ' ' . $table . ' ');
        if ($condition)
            $this->setSql('ON ' . $condition . ' ');
    }

    public function leftJoin($table, $condition = '') {
        $this->setJoinType('LEFT JOIN', $table, $condition);
        return $this;
    }

    public function rightJoin($table, $condition = '') {
        $this->setJoinType('RIGHT JOIN', $table, $condition);
        return $this;
    }

    public function where2($field, $operator, $value) {
        if (!strstr($this->getSql(), 'WHERE'))
            $this->setSql(
                    ' WHERE ' . $field . ' ' . $operator . ' ' .
                    (is_numeric($value) ? $value : '\'' . $value . '\'')
            );
        return $this;
    }

    public function where($field, $value) {
        if (!strstr($this->getSql(), 'WHERE'))
            $this->setSql(
                    ' WHERE ' . $field . '=' .
                    (is_numeric($value) ? $value : '\'' . $value . '\'')
            );
        return $this;
    }

    protected function multipleWhere($conditions, $operator) {
        $this->setSql(!strstr($this->getSql(), 'WHERE') ? ' WHERE ' : " $operator ");

        foreach ($conditions as $field => $value)
            $whereSql .= ($whereSql ? " $operator " : ' ')
                    . $field . '=' . (is_numeric($value) ? $value : '\'' . $value . '\'');

        $this->setSql($whereSql);

        return $this;
    }

    public function andWhere(array $conditions) {
        return $this->multipleWhere($conditions, 'AND');
    }

    public function orWhere(array $conditions) {
        return $this->multipleWhere($conditions, 'OR');
    }

    public function orderBy($fields, $order = 'ASC') {
        if (is_array($fields)) {
            foreach ($fields as $field)
                $orderFields .= $field . ',';
            $this->setSql(' ORDER BY ' . substr($orderFields, 0, -1) . ' ' . $order);
        } else {
            $this->setSql(' ORDER BY ' . $fields . ' ' . $order);
        }
        return $this;
    }

    public function groupBy($fields) {
        if (is_array($fields)) {
            foreach ($fields as $field)
                $orderFields .= $field . ',';
            $this->setSql(' GROUP BY ' . substr($orderFields, 0, -1));
        } else {
            $this->setSql(' GROUP BY ' . $fields);
        }
        return $this;
    }

    public function limit($param1, $param2 = '') {
        $limit = ' LIMIT ' . $param1 . ' ';
        if ($param2)
            $limit .= ',' . $param2 . ' ';
        $this->setSql($limit);
        return $this;
    }

    public function execute($sql = '') {
        $result = mysql_query($sql ? $sql : $this->getSql(), $this->getConnection());
        if (!$result) {
            $this->log('Query [' . mysql_error($this->getConnection()) . ']', 500);
            return false;
        } else
            return $result;
    }

}

?>